//
//  GenerateQrCodeController.swift
//  WeiBoSwift
//
//  Created by Jacky Sun on 16/4/26.
//  Copyright © 2016年 syf. All rights reserved.
//

import UIKit
import SnapKit
class GenerateQrCodeController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
       QRIMG.image =  generateQRCode()
       view.addSubview(QRIMG)
        
        
        // 使用约束
        QRIMG.snp_makeConstraints { (make) in
          make.center.equalTo(view)
          make.width.height.equalTo(200)
            
        }
    }

    //MARK: - 生成二维码
    func generateQRCode() ->UIImage{
        
        let filter = CIFilter(name: "CIQRCodeGenerator")
        // 2、恢复滤镜的默认属性
        filter?.setDefaults()
        
        //3、 将字符串转换成NSData
        let data: NSData = "Jacky Sun".dataUsingEncoding(NSUTF8StringEncoding)!
        
        //4、通过KVO设置滤镜， 传入data,将来滤镜就知道通过传入数据生成二维码
        filter?.setValue(data, forKey: "inputMessage")
        
        // 5、生成二维码
        let img: CIImage =  filter!.outputImage!
         let img_HD =  createNonInterpolatedUIImageFormCIImage(img, size: 200)
        
        let iconImg = UIImage(named: "rr")
        
        let newImage = createNewIMg(img_HD, iconIMg: iconImg!)
        
        return newImage
    }

    
    
    /**
     合成二维码 和用户icon
     
     - parameter qrIMg:   qrIMg description
     - parameter iconIMg: <#iconIMg description#>
     
     - returns: <#return value description#>
     */
    private func createNewIMg(qrIMg: UIImage, iconIMg: UIImage) ->UIImage {
     
        //1、开启图片上下文
        UIGraphicsBeginImageContext(qrIMg.size)
        
        //2、 绘制背景图片
        qrIMg.drawInRect(CGRect(origin: CGPointZero, size: qrIMg.size))
        
        //3、 绘制头像
        let width: CGFloat = 50
        let height: CGFloat = width
        let x = (qrIMg.size.width - width) * 0.5
        let y = x
        iconIMg.drawInRect(CGRect(x: x, y: y, width: width, height: height))
        
        //4'\ 取出绘制好的图片
        let newIMg =  UIGraphicsGetImageFromCurrentImageContext()
        
        //5\ 关闭图形上下文
        UIGraphicsEndImageContext()
        
        return newIMg
        
    }
    
    
  
    /**
     根据CIImage生成指定大小的高清UIImage
     
     :param: image 指定CIImage
     :param: size    指定大小
     :returns: 生成好的图片
     */
    private func createNonInterpolatedUIImageFormCIImage(image: CIImage, size: CGFloat) -> UIImage {
        
        let extent: CGRect = CGRectIntegral(image.extent)
        let scale: CGFloat = min(size/CGRectGetWidth(extent), size/CGRectGetHeight(extent))
        
        // 1.创建bitmap;
        let width = CGRectGetWidth(extent) * scale
        let height = CGRectGetHeight(extent) * scale
        let cs: CGColorSpaceRef = CGColorSpaceCreateDeviceGray()!
        let bitmapRef = CGBitmapContextCreate(nil, Int(width), Int(height), 8, 0, cs, 0)!
        
        let context = CIContext(options: nil)
        let bitmapImage: CGImageRef = context.createCGImage(image, fromRect: extent)
        
        CGContextSetInterpolationQuality(bitmapRef,  CGInterpolationQuality.None)
        CGContextScaleCTM(bitmapRef, scale, scale);
        CGContextDrawImage(bitmapRef, extent, bitmapImage);
        
        // 2.保存bitmap到图片
        let scaledImage: CGImageRef = CGBitmapContextCreateImage(bitmapRef)!
        
        return UIImage(CGImage: scaledImage)
    }

        ///  懒加载 img
    private lazy var QRIMG: UIImageView = {
//        let img = UIImageView(frame: CGRect(x: 100, y: 100, width: 200, height: 200))
//        return img
        
        
        // 使用约束
        let img = UIImageView()
        return img
    }()
    
}
